#coding:utf-8
__author__ = 'Administrator'

import tornado.ioloop
import tornado.web
import random

ROOM_INFO = {}
PLAYER_SETTINGS ={
    '5': {'狼人': '2', '平民': '2', '预言家': '1'},
    '6': {'狼人': '2', '平民': '2', '预言家': '1', '女巫': '1'},
    '7': {'狼人': '3', '平民': '2', '预言家': '1', '女巫': '1'},
    '8': {'狼人': '3', '平民': '2', '预言家': '1', '女巫': '1', '猎人': '1'},
    '9': {'狼人': '3', '平民': '3', '预言家': '1', '女巫': '1', '猎人': '1'},
    '10': {'狼人': '4', '平民': '3', '预言家': '1', '女巫': '1', '猎人': '1'},
    '11': {'狼人': '4', '平民': '4', '预言家': '1', '女巫': '1', '猎人': '1'},
    '12': {'狼人': '4', '平民': '5', '预言家': '1', '女巫': '1', '猎人': '1'},


}

settings = {
    'static_path': 'static',
    'template_path': 'template',
    'static_url_prefix': '/static/'
}


class MaiHandler(tornado.web.RequestHandler):
    def get(self):
        # self.write("Hello,world!")
        self.render('base.html')


class CreateGameHandler(tornado.web.RequestHandler):
    def post(self, *args, **kwargs):
        # get num of players
        player_num = self.get_argument('player_num')
        print(player_num)
        player_list = []
        player_num = int(player_num)
        # get current role setting
        player_setting = PLAYER_SETTINGS[str(player_num)]
        # create desk of player_num
        for role in player_setting:
            for i in range(int(player_setting[role])):
                player_list.append({'role': role,
                'player': "还没加入",
                'person_id': ''})
        random.shuffle(player_list)
        # create room_num
        while True:
            room_num = str(random.randint(1, 1000))
            if room_num not in ROOM_INFO:
                break
        # add room num to roo_info
        ROOM_INFO[room_num] = {
            'players': player_list,
            'judge': '房主',
        }
        print(ROOM_INFO)

def make_app():
    return tornado.web.Application([
        (r"/", MaiHandler),
        (r'/create_game', CreateGameHandler),

    ], **settings)

if __name__ == '__main__':
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()
